﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CNVP.Config;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (FileUtils.FolderExists(Server.MapPath(@"~"+Config.UIConfig.InstallDir+"Install/")))
            {
                Response.Redirect(Config.UIConfig.InstallDir+"Install/Index.aspx", true);
            }
            if (!IsPostBack)
            {
                var action = Request.Params["action"];
                var file = Request.Params["file"];
                string cid = Request.Params["cid"];
                string pid=Request.Params["pid"];
                string order = Request.QueryString["order"];
                int tTypeID;
                int topID=0;
                if (String.IsNullOrEmpty(cid) || (!Public.IsNumber(cid)))
                { cid = "0"; }


                bool canEdit = (action == "design");
                int ModuleType = 0;
                file = (String.IsNullOrEmpty(file) ? "default" : file);
                file = file.ToLower();
                switch (file)
                {
                    case "default":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        tTypeID = 0;
                        break;
                    case "list":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        ModuleType = 3;
                        tTypeID = 1;
                        if (canEdit && (String.IsNullOrEmpty(cid) && !Public.IsNumber(cid)) )
                        {
                            cid = new BLL.CNVP_Column().GetTopColumnID(ModuleType);
                        }
                        if(2==(new BLL.CNVP_Column().GetColoumnTemplateType(Convert.ToInt32(cid))))
                        {
                            topID = new BLL.CNVP_Products().GetTopProductDetailID(Convert.ToInt32(cid));
                        }
                        break;
                    case "show":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        ModuleType = 3;
                        tTypeID = 2;

                        if (canEdit && (String.IsNullOrEmpty(cid) && !Public.IsNumber(cid)) )
                        {
                            cid = new BLL.CNVP_Column().GetTopColumnID(ModuleType);
                        }
                        if(2==(new BLL.CNVP_Column().GetColoumnTemplateType(Convert.ToInt32(cid))))
                        {
                            topID = new BLL.CNVP_Products().GetTopProductDetailID(Convert.ToInt32(cid));
                        }

                        break;
                    case "search":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        ModuleType = 3;
                        tTypeID = 3;
                        break;
                    case "nindex":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        ModuleType = 2;
                        tTypeID = 4;
                        break;
                    case "nlist":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        ModuleType = 2;
                        tTypeID = 5;
                        if (canEdit && (String.IsNullOrEmpty(cid) && !Public.IsNumber(cid)))
                        {
                            cid = new BLL.CNVP_Column().GetTopColumnID(ModuleType);
                        }
                        if (6 == (new BLL.CNVP_Column().GetColoumnTemplateType(Convert.ToInt32(cid))))
                        {
                            tTypeID = 6;
                            topID = new BLL.CNVP_NewsInfo().GetTopNewsDetailID(Convert.ToInt32(cid));
                        }
                        break;
                    case "nshow":
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        ModuleType = 2;
                        tTypeID = 6;

                        if (canEdit && (String.IsNullOrEmpty(cid) && !Public.IsNumber(cid)))
                        {
                            cid = new BLL.CNVP_Column().GetTopColumnID(ModuleType);
                        }
                        if (6 == (new BLL.CNVP_Column().GetColoumnTemplateType(Convert.ToInt32(cid))))
                        {
                            tTypeID = 6;
                            topID = new BLL.CNVP_NewsInfo().GetTopNewsDetailID(Convert.ToInt32(cid));
                        }

                        break;
                    default:
                        #region 判断数据缓存
                        if (!canEdit && UIConfig.AutoCache == "1")
                        {
                            if (CNVP.Cache.CacheUtils.FileCacheProgress()) { return; }
                        }
                        #endregion
                        file = "default";
                        tTypeID = 0;
                        break;
                }



                BLL.CNVP_Template bll = new BLL.CNVP_Template();
                DataTable Dt = new DataTable();
                CNVP.JShop.BLL.CNVP_Column bll_CNVP_Column = new BLL.CNVP_Column();
                DataTable mdt_Column = new DataTable();
                if (Convert.ToInt32(cid) != 0)
                {
                    bll_CNVP_Column = new BLL.CNVP_Column();
                    mdt_Column = new DataTable();
                    mdt_Column = bll_CNVP_Column.GetColumnInfo(Convert.ToInt32(cid));
                }
                if ((mdt_Column == null && mdt_Column.Rows.Count == 0) || Convert.ToInt32(cid) == 0)
                {
                    Dt = bll.GetTemplateInfo(0, tTypeID, UIConfig.TemplateUse);
                }
                else
                {
                    int columnTemplateType = 1;
                    if (String.IsNullOrEmpty(mdt_Column.Rows[0]["TemplateUrl"].ToString()) || !Public.IsNumber(mdt_Column.Rows[0]["TemplateUrl"].ToString()))
                    {
                        columnTemplateType = 1;
                    }
                    else
                    {
                        columnTemplateType = Convert.ToInt32(mdt_Column.Rows[0]["TemplateUrl"].ToString());
                    }
                    if (tTypeID == 1 || tTypeID == 5)
                    {
                        tTypeID = columnTemplateType;
                    }
                    Dt = bll.GetTemplateInfo(mdt_Column.Rows[0]["AddressRoute"].ToString() + "," + mdt_Column.Rows[0]["ID"].ToString(), tTypeID, UIConfig.TemplateUse);
                }

                if (Dt != null && Dt.Rows.Count > 0)
                {
                    string TemplateType = tTypeID.ToString();
                    string TemplateUrl = Dt.Rows[0]["TemplateUrl"].ToString();
                    string ColumnID = Dt.Rows[0]["ColumnID"].ToString();
                    if (!FileUtils.FileExists(Server.MapPath(TemplateUrl)))
                    {
                        Response.Write("模板文件不存在！");
                        Response.End();
                    }
                    string strTemplate = FileUtils.ReadFile(Server.MapPath(TemplateUrl));

                    if (canEdit)
                    {
                        //判断页面权限
                        HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + ""];
                        HttpCookie Cookie_AdminID = Request.Cookies["" + UIConfig.CookiesName + "AdminID"];
                        HttpCookie Cookie_RoleID = Request.Cookies["" + UIConfig.CookiesName + "RoleID"];
                        if (Cookie == null || Cookie_AdminID == null || Cookie_RoleID == null)
                        {
                            MessageUtils.Write("<script>top.location.href=\"" +Config.UIConfig.InstallDir+ Config.UIConfig.AdminDir  + "/Login.aspx\"</script>");

                        }

                        Response.Write("<html>");
                        Response.Write("<head>\r\n<title>模板管理 - 单元设置</title>\r\n");
                        Response.Write("<base href='http://" + Request.Url.Authority + @"/Template/Default/' />"+"\r\n");
                        Response.Write("<link type=\"text/css\" href=\"/admin/common/thickbox.css\" rel=\"stylesheet\">\r\n");
                        Response.Write("<script language=\"javascript\" src=\"/Scripts/jquery.js\"></script>\r\n");
                        Response.Write("<script language=\"javascript\" src=\"/admin/common/thickbox.js\"></script>\r\n");
                        Response.Write("<script language=\"javascript\" src=\"/admin/common/option.js\"></script>\r\n");
                       
                        Response.Write("<script language=\"javascript\" src=\"/admin/Template/editmenu.js\"></script>\r\n");
                        Response.Write("</head>\r\n");
                        Response.Write("<body style=\"margin:0px;padding:0px;font-size:12px;width:100%\">\r\n");
                    }
                    CNVP.JShop.Publish.Template bll1 = new CNVP.JShop.Publish.Template();
                    bll1.TemplatePublish = false;
                    bll1.ProjectName = UIConfig.TemplateUse;
                    bll1.TemplateContent = strTemplate;
                    bll1.ModuleType = ModuleType;
                    bll1.TemplateType = Convert.ToInt32(TemplateType);
                    bll1.ColumnID = cid;
                    if (tTypeID == 2 || tTypeID == 6)
                    {
                        if (String.IsNullOrEmpty(cid) || (!Public.IsNumber(cid)))
                        {
                            bll1.DetailID = topID; 
                        }
                        else
                        {
                            if (String.IsNullOrEmpty(pid) || (!Public.IsNumber(pid)))
                            {
                                bll1.DetailID = topID;
                            }
                            else
                            {
                                bll1.DetailID = Convert.ToInt16(pid);
                            }
                        }
                    }
                    else
                    { bll1.DetailID = topID; }

                    bll1.ReplaceLabels(canEdit);
                    if (!canEdit)
                    {
                        bll1.TemplateContent = bll1.TemplateContent.Replace(@"<head>", @"<head>" + "\r\n" + "<base href='http://" + HttpContext.Current.Request.Url.Authority + @"/Template/Default/' />"+ "\r\n");
                    }
                    #region 智能压缩HTML
                    if (UIConfig.GZipHtml == "1")
                    {
                        Response.Write(Public.GZipHtml(bll1.TemplateContent));
                    }
                    else
                    {
                        Response.Write(bll1.TemplateContent);
                    }
                    #endregion
                    if (canEdit)
                    {
                        Response.Write("<input name=\"isAvailMenu\" value=\"0\" type=\"hidden\">\r\n");
                        Response.Write("<input name=\"copyobject\" type=\"hidden\">\r\n");
                        Response.Write("<iframe name=\"delete_window_ifram\" width=\"0\" height=\"0\"></iframe>\r\n");
                        Response.Write("<input value=\"\" id=\"ColumnID\" name=\"ColumnID\" type=\"hidden\">\r\n");
                        Response.Write("<input value=\"\" id=\"UnitID\" name=\"UnitID\" type=\"hidden\">\r\n");
                        Response.Write("<input value=\"\" id=\"UnitName\" name=\"UnitName\" type=\"hidden\">\r\n");
                        Response.Write("<input value=\"\" id=\"StyleID\" name=\"StyleID\" type=\"hidden\">\r\n");
                        Response.Write("<!-- 单元类型：0首页，1列表，2内页 -->\r\n");
                        Response.Write("<input value=\"\" id=\"ModalType\" name=\"ModalType\" type=\"hidden\">\r\n");
                        Response.Write("</body>\r\n");
                        Response.Write("</html>\r\n");
                    }
                    Response.Write("<!--Publish By JShop2011(捷点科技) " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-->");
                    #region 写入数据缓存
                    if (!canEdit && UIConfig.AutoCache == "1")
                    {
                        Response.Write("<!--采用数据缓存-->");
                        CNVP.Cache.CacheUtils.FileCacheRewrite();
                    }
                    #endregion
                }
                Response.End();

            }
        }
    }
}
